package com.gk.courses.dao.mock;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.gk.courses.model.Course;
import com.gk.courses.model.Student;

public class StorageMock {

	private final Map<Integer, List<Course>> student2course = new HashMap<Integer, List<Course>>();
	private final Map<Integer, List<Student>> course2student = new HashMap<Integer, List<Student>>();
	private final Map<Integer, List<Course>> student2completedCourse = new HashMap<Integer, List<Course>>();
	private final Map<Integer, Student> students = new HashMap<Integer, Student>();
	private final Map<Integer, Course> courses = new HashMap<Integer, Course>();

	public void clear() {
		student2course.clear();
		course2student.clear();
		student2completedCourse.clear();
		students.clear();
		courses.clear();
	}

	public void setStudent(Student... student) {
		for (Student _student : student) {
			students.put(_student.getId(), _student);
		}
	}

	public void setCourse(Course... course) {
		for (Course _course : course) {
			courses.put(_course.getCourseId(), _course);
		}
	}

	public Student getStudent(Integer studentId) {
		return students.get(studentId);
	}

	public Course getCourse(Integer courseId) {
		return courses.get(courseId);
	}

	public List<Course> getCourses() {
		return new ArrayList<Course>(courses.values());
	}

	public Set<Student> getStudents() {
		return new HashSet<Student>(students.values());
	}

	public void enrollStudentInCourse(Student student, Course course) {
		enrollStudentInCourse(student.getId(), course);
	}

	public void enrollStudentInCourse(Integer studentId, Course course) {
		List<Course> courseList = student2course.get(studentId);
		if (courseList == null) {
			courseList = new ArrayList<Course>();
			student2course.put(studentId, courseList);
		}
		courseList.add(course);
	}

	public List<Course> getEnrolledCoursesFor(Integer studentId) {
		List<Course> courses = student2course.get(studentId);
		if (courses == null) {
			return new ArrayList<Course>();
		}
		return courses;
	}

	public List<Student> getStudentsInCourse(Integer courseId) {
		List<Student> students = course2student.get(courseId);
		if (students == null) {
			return new ArrayList<Student>();
		}
		return students;
	}

	public List<Course> getCompletedCoursesFor(Integer studentId) {
		List<Course> courses = student2completedCourse.get(studentId);
		if (courses == null) {
			return new ArrayList<Course>();
		}
		return courses;
	}

	public void updateCompletedCourse(Course course, Student student) {
		List<Course> courses = getCompletedCoursesFor(student.getId());
		if (!courses.contains(course)) {
			courses.add(course);
		}
	}

}
